/*------------------------------------------------------------------------------*\
**   Copyright 2008 Jonhnny Weslley                                             **
**                                                                              **
**   Licensed under the Apache License, Version 2.0 (the "License");            **
**   you may not use this file except in compliance with the License.           **
**   You may obtain a copy of the License at                                    **
**                                                                              **
**       http://www.apache.org/licenses/LICENSE-2.0                             **
**                                                                              **
**   Unless required by applicable law or agreed to in writing, software        **
**   distributed under the License is distributed on an "AS IS" BASIS,          **
**   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   **
**   See the License for the specific language governing permissions and        **
**   limitations under the License.                                             **
\*------------------------------------------------------------------------------*/

package shibernate.singleton

import shibernate._
import org.hibernate.Session

object SearchAgent extends CriteriaLanguage {

  def listAll[A](entityClass: Class[A]) = Search(session, entityClass, null)

  def list[A](entityClass: Class[A])
          (value: Value) = Search(session, entityClass, value.param)

  def llist[A](entityClass: Class[A])
          (value: Value, firstResult: Int, maxResults: Int) = 
            Search(session, entityClass, value.param, firstResult, maxResults)

  def count[A](entityClass: Class[A])
           (value: Value) = Search.resultCount(session, entityClass, value.param)

  def list[A](value: Value)
          (entityClass: Class[A]) = Search(session, entityClass, value.param)

  def list[A](value: Value)
          (entityClass: Class[A], firstResult: Int, maxResults: Int) = 
            Search(session, entityClass, value.param, firstResult, maxResults)

  def count[A](value: Value)
           (entityClass: Class[A]) = Search.resultCount(session, entityClass, value.param)

  private def session = SessionManager.getSession    


// Implicit definitions  

  implicit def implicitList[A](entityClass: Class[A]) = new ImplicitSearch(entityClass)

  class ImplicitSearch[A](entityClass: Class[A]) {

    def apply() = {
      Search(session, entityClass, null)
    }

    def apply(value: Value) = {
      Search(session, entityClass, value.param)
    }

    def list = this()

    def list(value: Value) = this(value)

    def ?: (value: Value) = this(value)

  }

}
